package logic

import (
	"context"
	"fmt"
	"inventory-system/model"
	"inventory-system/pkg"

	"inventory-system/systemrpc/internal/svc"
	"inventory-system/systemrpc/systemrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type LoginLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginLogic {
	return &LoginLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *LoginLogic) Login(in *systemrpc.LoginRequest) (*systemrpc.LoginResponse, error) {
	// todo: add your logic here and delete this line
	var user model.User
	model.DB.Where("account=?", in.Account).Find(&user)
	if user.ID == 0 {
		return nil, fmt.Errorf("用户未注册")
	}
	if user.Password != pkg.Md5str(in.Password) {
		return nil, fmt.Errorf("密码错误")
	}
	return &systemrpc.LoginResponse{
		UserId: int64(user.ID),
	}, nil
}
